// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
// import './assets/css/mainv2.css';
import axios from 'axios';
import JsonViewer from 'vue-json-viewer';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

import apiConfig from '../config/api.config'
import VueAxios from 'vue-axios'

import qs from "qs";

Vue.use(ElementUI);
Vue.use(JsonViewer);
Vue.use(VueAxios, axios);
axios.defaults.baseURL = apiConfig.baseUrl;



//设置cookie,放在main.js
Vue.prototype.setCookie = function (c_name, value, expiredays) {
  var exdate = new Date()
  exdate.setDate(exdate.getDate() + expiredays)
  document.cookie = c_name + "=" + escape(value) +
    ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString())
};

//获取cookie
Vue.prototype.getCookie = function (c_name) {
  if (document.cookie.length > 0) {
    var c_start = document.cookie.indexOf(c_name + "=")
    if (c_start != -1) {
      c_start = c_start + c_name.length + 1
      var c_end = document.cookie.indexOf(";", c_start)
      if (c_end == -1) c_end = document.cookie.length
      return unescape(document.cookie.substring(c_start, c_end))
    }
  }
  return ""
};

//删除所有cookie
Vue.prototype.delCookie = function (c_name) {
  // console.log(c_name);
  // var exp = new Date();
  // exp.setTime(exp.getTime() - 1);
  // var cval = this.getCookie(c_name);
  // if (cval != null)
  //   document.cookie = c_name + "=" + cval + ";expires=" + exp.toGMTString();
  var keys = document.cookie.match(/[^ =;]+(?==)/g)
  if (keys) {
    for (var i = keys.length; i--;) {
      document.cookie = keys[i] + '=0;path=/;expires=' + new Date(0).toUTCString() // 清除当前域名下的,例如：m.ratingdog.cn
      document.cookie = keys[i] + '=0;path=/;domain=' + document.domain + ';expires=' + new Date(0).toUTCString() // 清除当前域名下的，例如 .m.ratingdog.cn
      document.cookie = keys[i] + '=0;path=/;domain=yzhsae.com;expires=' + new Date(0).toUTCString() // 清除一级域名下的或指定的，例如 .ratingdog.cn
    }
  }
}





Vue.prototype.download = function (id) {
  console.log("idddd:", id);
  var that = this;
  if (that.getCookie("user")) {
    that.user = JSON.parse(that.getCookie("user"));
    console.log("user:", that.user);
    var data = {
      appId: id,
      userId: that.user.id
    };
    console.log("datadatadata:", data);

    that.$http({
      url: `/api/appstore/app/downLoad`,
      method: "post",
      headers: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: qs.stringify(data)
    })
      .then(res => {
        if (res.data.code == "1") {
          window.open(res.data.datas, "_self");
        } else {
          console.log(333333333);
          alert(res.data.msg);
          // that.formmsg = res.data.msg;
        }
      })
      .catch(function (error) {
        console.log(123213313);
      });
    // console.log("111111:", data);
  } else {
    that.tcxs = !that.tcxs;
  }
}



// Vue.config.productionTip = false
// Vue.prototype.$http = axios
// Vue.prototype.$http.defaults.baseURL = 'http://192.168.23.44:9023' // `baseURL` 将自动加在 `url` 前面，除非 `url` 是一个绝对 URL

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
